<?php
/**
 * @author guoxiaoyong
 * H5 活动
 */
namespace frontend\controllers;

//引入类
use bc\controller\FrontController;
use bc\model\h5\ActivityModel;
use yii\web\Session;
use bc\service\DictCityService;
use bc\service\DictDistrictService;
use bc\service\DictProvinceService;
use bc\model\h5\PrizeModel;
use bc\model\h5\LuckmoneyModel;

class ActivityController extends FrontController
{
	
	
	protected $prizes = array(0 => '8元优惠券', 1 => '18元优惠券', 2=> '28元优惠券', 3=> '58元优惠券', 4=>'精美礼品一份', 5=> 'iPhone6s 一台');
	
	/**
	 * 活动首页
	 */
	public function actionIndex()
	{
		$this->setOAuth();
		
		$session = new Session();
		$user = $session->get('user');
		if(!$user)
			$this->redirect('/');
		
		
		$view = [];
		return $this->render('index', $view);
	}
	
	/** 
	 * 
	 * 活动结果页
	 */
	public function actionResult()
	{
		$session = new Session();
		$user = $session->get('user');
		if(!$user)
			$this->redirect('/');
		
		//分值
		$value = $this->CalculationResult();
		$view = [];
		
		$gender = $this->get('gender');//1:男宝宝 2：女宝宝 3：未知

		//写入数据表
		$activityModel = new ActivityModel();
	
		$data['nickname'] = $user['nickname'];
		$data['openid'] = $user['openid'];
		$data['val'] = $value;
		$data['gender'] = $gender;//性别
		$data['create_time'] = time();
		$id = $activityModel->add($data);
		
		
		//生成js sdk签名
		$jsConfig = $this->configJsApi();
		
		$appid = $this->wechat()->appid;
		$secret = $this->wechat()->secret;
		$redirect_uri = urlencode('http://wx.zhishimama.com/activity/share?id='.$id);
		$share_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&#wechat_redirect";
		
		
		
		
		$description = $this->getDescription($gender, $value);
		
		$share_title = mb_substr($description['content'], 0, 30) ;
		
		$view['user'] = $user;
		$view['value'] = $value;
		$view['gender'] = $gender;
		$view['share_url'] = $share_url;
		$view['js_config'] = $jsConfig;
		$view['description'] = $description;
		$view['share_title'] = $share_title;
		
		return $this->render('result', $view);
		
	}
	
	/**
	 * 分享页
	 */
	public function actionShare()
	{
		$auth = $this->get('auth', '');
		if($auth == 'no')
		{
			$msg = '再玩一次';
		}
		else 
		{
			$this->setOAuth();
			$msg = '我要参加';
		}
		
		$id = $this->get('id');
		if(!$id)
			$this->redirect('/');
		
		$activityModel = new ActivityModel();
		$row = $activityModel->getRowById($id);
		
		$description = $this->getDescription($row['gender'], $row['val']);
		
		$view['msg'] = $msg;
		$view['row'] = $row;
		$view['description'] = $description;
		
		return $this->render('share', $view);
		
	}
	
	/**
	 * ajax 分享用户有抽奖资格
	 */
	public function actionAjaxdolottery()
	{
		
	}
	
	/**
	 * 拿优惠券50元
	 */
	public function actionCoupon()
	{
		$session = new Session();
		$user = $session->get('user');
		if(!$user)
			$this->redirect('/');
		
		
		$luckmoneyModel = new LuckmoneyModel();
		$row = $luckmoneyModel->getRow(['openid' => $user['openid']]);
		if(!$row)
		{
			$data['openid'] = $user['openid'];
			$data['create_time'] = time();
			$luckmoneyModel->add($data);
		}
		
			
		$view['row'] = $row;
		return $this->render('coupon', $view);
	}


	
	/**
	 * 领取优惠券动作
	 */
	public function actionDocoupon()
	{
		//执行抽奖
		$session = new Session();
		$user = $session->get('user');
		if($user)
		{
			$prizeModel = new PrizeModel();
			$row = $prizeModel->getRow(['openid' => $user['openid']]);
			
			if(!$row)
			{
				$data['nickname'] = $user['nickname'];
				$data['openid'] = $user['openid'];
				$data['create_time'] = time();
				$lottery = $this->doLottery();
				
				if($user['openid'] == 'oUMV0swB27mi5oApqVFha0Afh-1Q')
				{
					$lottery  = 4;
				}
				
				if($lottery >= 4)
				{
					$data['prize_type'] = 2;//实物
				
				}
				else
				{
					$data['prize_type'] = 1;//虚拟
				}
					
				$prize = $this->prizes[$lottery];
				$data['prize_content'] = $prize;
				
				$prizeModel->add($data);
				$view['row'] = $data;
			}
			else 
			{
				$view['row'] = $row;
				$data['msg'] = '您已抽过奖了!';
			}
			return $this->render('docoupon', $view);
			
		}
		
	}
	
	
	/**
	 * 实物奖品用户信息填写页
	 */
	public function actionInfo()
	{
		$session = new Session();
		$user = $session->get('user');
		
		if(!$user)
			$this->redirect('/');
		
		
		$prizeModel = new PrizeModel();
		$row = $prizeModel->getRow(['openid' => $user['openid']]);
		
		if($row['prize_type'] != 2)
			$this->redirect('/');
		
		//获取省
		$provinceService = new DictProvinceService();
		$provinceList = $provinceService->getList();
		
		$view['province'] = $provinceList;
		return $this->render('info', $view);
	}
	
	
	/**
	 * 保存数据
	 */
	public function actionSave()
	{
		$session = new Session();
		$user = $session->get('user');
		
		if(!$user)
			$this->redirect('/');
		
		$prizeModel = new PrizeModel();
		$row = $prizeModel->getRow(['openid' => $user['openid']]);
		
		if($row['prize_typ'] != 2)
			$this->redirect('/');
		

		$city_id = $this->post('city');
		$district_id = $this->post('district');
		$province_id = $this->post('province');
		
		$provinceService = new DictProvinceService();
		$province  = $provinceService->getRow(['id' => $province_id]);
		
		$cityService = new DictCityService();
		$city = $cityService->getRow(['id' => $city_id]);
		
		$districtService = new DictDistrictService();
		$district = $districtService->getRow(['id' => $district_id]);
		
		
		$data['province_name'] = $province['province_name'];
		$data['city_name'] = $city['city_name'];
		$data['district_name'] = $district['district_name'];
		
		
		$data['address'] = $this->post('address');
		$data['contact'] = $this->post('name');
		
		$activityModel = new ActivityModel();
		$list = $activityModel->getList(['openid' => $user['openid']]);
		
		$result = $list[0];
		
		$prizeModel->modify($data, $row['prize_id']);
		$url = '/activity/share?auth=no&id='.$result['id'];
		
		echo '<script>alert("提交成功！");window.location.href="' . $url . '"</script>';
		
		
		$this->redirect( );
		
	}
	
	public function actionTest()
	{
		return $this->render('test');
	}
	
	/**
	 * 返回分值
	 */
	private function CalculationResult()
	{
		$questions = $this->questionBank();
		
		$q1 = $this->get('q1');
		$q2 = $this->get('q2');
		$q3 = $this->get('q3');
		$q4 = $this->get('q4');
		$q5 = $this->get('q5');
		
		
		$q1_val = $questions[1][$q1];
		$q2_val = $questions[2][$q2];
		$q3_val = $questions[3][$q3];
		$q4_val = $questions[4][$q4];
		$q5_val = $questions[5][$q5];
		
		$resultVal = intval($q1_val + $q2_val + $q3_val + $q4_val + $q5_val);
		
		return $resultVal;
	}
	
	/**
	 * @return array
	 */
	private function questionBank()
	{
		$question = [];
		//题目1
		$question[1][1] = 21;
		$question[1][2] = 12;
		$question[1][3] = 7;
		$question[1][4] = 0;
		
		//题目2
		$question[2][1] = 21;
		$question[2][2] = 8;
		$question[2][3] = 0;
		
		//题目3
		$question[3][1] = 21;
		$question[3][2] = 12;
		$question[3][3] = 7;
		$question[3][4] = 0;
		
		//题目4
		$question[4][1] = 21;
		$question[4][2] = 8;
		$question[4][3] = 0;
		
		//题目5
		$question[5][1] = 21;
		$question[5][2] = 8;
		$question[5][3] = 0;
		
		return $question;
	}
	
	
	/**
	 * 执行抽奖程序
	 * @return integer
	 */
	private function doLottery()
	{
		$rand = rand(1, 1000);
		
		$r = 0;
		
		if($rand <= 700)
		{
			// 70%
			$r = 0;
		}
		else if ($rand > 700 && $rand <= 000 )
		{
			// 20%
			$r = 1;
		}
		else if ($rand > 900 && $rand <= 950)
		{
			// 5%
			$r = 2;
		}
		else if($rand > 950 && $rand <= 999)
		{
			// 9.9%
			$r = 3;
		}
		else if($rand == 100)
		{
			//实物 0.1%
			$r = 4;
		}
		else
		{
			$r = 0;
		}
		return $r;
	}
	
	private function getDescription($type, $val)
	{
		$description = [];
		if($type == 1)//男性
		{
			if($val >= 0 && $val <= 15)
			{
				$description[0]['title'] = '天神';
				$description[1]['title'] = '宙斯';
				$description['content'] = '占有欲极强，并且拥有相当的内涵，做事认真，超强的生存概念，让他能够很快地适应环境，属于社交高段的类型。所以，您的孩子，能当领导。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/1.png';
			}
			else if($val > 15 && $val <= 30)
			{
				$description[0]['title'] = '冥王';
				$description[1]['title'] = '哈迪斯';
				$description['content'] = '您的孩子仿佛有隐形斗篷般的，默默地潜藏在每件重要事情背后，不高调却在重要关头展现关键的影响力，性格温和不与人争执，压抑自己的性格，期许自己可以陶冶出人应有的所有美德。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/2.png';
			}
			else if($val > 30 && $val <= 45)
			{
				$description[0]['title'] = '海神';
				$description[1]['title'] = '波赛顿';
				$description['content'] = '耍得了谋略，开得了脑洞，喜欢创作记忆，对于真理、是非对错有洁癖，工作中常常会受到重用，但性格偏情绪化，是忠实呈现情绪不隐瞒的好朋友。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/3.png';
			}
			else if($val > 45 && $val <= 60)
			{
				$description[0]['title'] = '太阳神';
				$description[1]['title'] = '阿波罗';
				$description['content'] = '您的孩子擅长掌管各方，对于自我认知透彻，追求感情的安稳，每件事情都尽全力协调平衡，面对感情却可能失去理智，他是英雄也需要感情滋润的最佳代表。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/4.png';
			}
			else if($val > 60 && $val <= 75)
			{
				$description[0]['title'] = '信使之神';
				$description[1]['title'] = '贺密斯';
				$description['content'] = '强大的学习能力，十足的聪明头脑，无比恶搞的心态，让他成为孩子王？捣蛋鬼？小说男主？亦或者是学霸？学霸？！';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/5.png';
			}
			else if($val > 75 && $val <= 90)
			{
				$description[0]['title'] = '火神';
				$description[1]['title'] = '贺菲斯托斯';
				$description['content'] = '默默努力，绝佳的耐心雕塑成完美的能力，在自己独霸的领域里影响力甚大，许多人望其项背。记性强、果敢坚忍，重视承诺，十足的大好人。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/6.png';
			}
			else if($val >= 90)
			{
				$description[0]['title'] = '战神';
				$description[1]['title'] = '艾瑞斯';
				$description['content'] = '十足的破坏大王，有着非凡的勇气和强大的破坏力，性格有点自恋，自带追光灯，没错，将来的施瓦辛格就是你！';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nan/7.png';
			}
		}
		else if($type == 2)//女性
		{
			if($val >= 0 && $val <= 15)
			{
				$description[0]['title'] = '月亮女神';
				$description[1]['title'] = '狄安娜';
				$description['content'] = '绝美的容貌不能妨碍她那种想要成功、得到肯定的决心，希望自己有一天也能得到诺大的影响力，为此不断训练自己，努力不懈将会有一番崭新无前例的作为。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/1.png';
			}
			else if($val > 15 && $val <= 30)
			{
				$description[0]['title'] = '爱与美女神';
				$description[1]['title'] = '维纳斯';
				$description['content'] = '您的孩子是天生的带领环境气氛高手，越多的强烈自信，就拥有越多掌握气氛的能力，凭借这样的能力，她很能够胜任业务性质、演艺事业、公关事业、与人亲近的工作等等，能够自然地成为她领域的佼佼者，同时追求者也是满地易寻。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/2.png';
			}
			else if($val > 30 && $val <= 45)
			{
				$description[0]['title'] = '丰收女神';
				$description[1]['title'] = '狄蜜特';
				$description['content'] = '患得患失的心态，导致您的孩子面对选择会时常出现焦虑。并且，伴随着一种杞人忧天的情怀，在生活的方方面面将会使得她更加谨小慎微。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/3.png';
			}
			else if($val > 45 && $val <= 60)
			{
				$description[0]['title'] = '婚嫁女神';
				$description[1]['title'] = '希拉';
				$description['content'] = '您的孩子是一个懂得善用所有能力来维护自己能力的人，她能够如此坦率的表达自己的占有欲，其实是对于爱情相当真诚的人，感性多于理性！';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/4.png';
			}
			else if($val > 60 && $val <= 75)
			{
				$description[0]['title'] = '月桂精灵';
				$description[1]['title'] = '达芙妮';
				$description['content'] = '您的孩子十分理性，更喜欢保守，安稳的状态生活，但当她遇到自己的意中人之后，将会付出自己的一切，幸福地享受爱情！';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/5.png';
			}
			else if($val > 75 && $val <= 90)
			{
				$description[0]['title'] = '智慧女神';
				$description[1]['title'] = '雅典娜';
				$description['content'] = '懂得谋略使用战术，也喜欢创作，对于正确、真理、是非对错有洁癖，谈感情用理智去比较与学习，面对工作一丝不苟的态度经常得到上司的青睐，受到重用。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/6.png';
			}
			else if($val >= 90)
			{
				$description[0]['title'] = '冥后';
				$description[1]['title'] = '波西凤';
				$description['content'] = '不断忍耐一切，只要决定跟随别人就死心塌地，工作、朋友、人际都配合度极高，需要知心好友透悉他的内心世界，才不会闷坏她敏感剔透易碎的心。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/nv/7.png';
			}
		}
		else if($type == 3)//未知
		{
			if($val >= 0 && $val <= 15)
			{
				$description[0]['title'] = '天神';
				$description[1]['title'] = '宙斯';
				$description['content'] = '占有欲极强，并且拥有相当的内涵，做事认真，超强的生存概念，让他能够很快地适应环境，属于社交高段的类型。所以，您的孩子，能当领导。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/1.png';
			}
			else if($val > 15 && $val <= 30)
			{
				$description[0]['title'] = '爱与美女神';
				$description[1]['title'] = '维纳斯';
				$description['content'] = '您的孩子是天生的带领环境气氛高手，越多的强烈自信，就拥有越多掌握气氛的能力，凭借这样的能力，她很能够胜任业务性质、演艺事业、公关事业、与人亲近的工作等等，能够自然地成为她领域的佼佼者，同时追求者也是满地易寻。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/2.png';
			}
			else if($val > 30 && $val <= 45)
			{
				$description[0]['title'] = '海神';
				$description[1]['title'] = '波赛顿';
				$description['content'] = '耍得了谋略，开得了脑洞，喜欢创作记忆，对于真理、是非对错有洁癖，工作中常常会受到重用，但性格偏情绪化，是忠实呈现情绪不隐瞒的好朋友。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/3.png';
			}
			else if($val > 45 && $val <= 60)
			{
				$description[0]['title'] = '太阳神';
				$description[1]['title'] = '阿波罗';
				$description['content'] = '您的孩子擅长掌管各方，对于自我认知透彻，追求感情的安稳，每件事情都尽全力协调平衡，面对感情却可能失去理智，他是英雄也需要感情滋润的最佳代表。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/4.png';
			}
			else if($val > 60 && $val <= 75)
			{
				$description[0]['title'] = '月桂精灵';
				$description[1]['title'] = '达芙妮';
				$description['content'] = '您的孩子十分理性，更喜欢保守，安稳的状态生活，但当她遇到自己的意中人之后，将会付出自己的一切，幸福地享受爱情！';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/5.png';
			}
			else if($val > 75 && $val <= 90)
			{
				$description[0]['title'] = '智慧女神';
				$description[1]['title'] = '雅典娜';
				$description['content'] = '懂得谋略使用战术，也喜欢创作，对于正确、真理、是非对错有洁癖，谈感情用理智去比较与学习，面对工作一丝不苟的态度经常得到上司的青睐，受到重用。';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/6.png';
			}
			else if($val >= 90)
			{
				$description[0]['title'] = '战神';
				$description[1]['title'] = '艾瑞斯';
				$description['content'] = '十足的破坏大王，有着非凡的勇气和强大的破坏力，性格有点自恋，自带追光灯，没错，将来的施瓦辛格就是你！';
				$description['icon'] = 'http://wx.zhishimama.com/img/result/unknow/7.png';
			}
		}
		
		return $description;
	}
}

